import { ElMessage } from "element-plus";

interface Params {
  fn: (...args: any[]) => Promise<any>;
  params: any[];
  success?: (res: any) => void;
  error?: (error: any) => void;
}

export async function request(params: Params) {
  try {
    const res = await params.fn(...params.params);
    if (res.code === 200) {
      params.success?.(res);
    } else {
      throw new Error(res.message);
    }
    return res;
  } catch (error: any) {
    ElMessage.error(error.message);
    params.error?.(error);
  }
}
